﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Microsoft.Win32;
using System.IO;
using Infrastructure;

using System.Windows.Forms;
using UserManager;
using Inspection.Views;
using Inspection.Models;

namespace Inspection
{
    public partial class MainWindow
    {
        private void AddCommand()
        {
            CommandBindings.Add(new CommandBinding(InitialCommand, Initial_Excute, Initial_CanExcute));
            CommandBindings.Add(new CommandBinding(RunCommand, Run_Excute, Run_CanExcute));
            CommandBindings.Add(new CommandBinding(StopCommand, Stop_Excute, Stop_CanExcute));
            CommandBindings.Add(new CommandBinding(LoginCommand, Login_Excute));
            CommandBindings.Add(new CommandBinding(PauseCommand, Pause_Excute, Stop_CanExcute));
            CommandBindings.Add(new CommandBinding(ResetCommand, ResetCommand_Excute, ResetCommand_CanExcute));
            CommandBindings.Add(new CommandBinding(OpenFileCommand, OpenFile_Excute));
            CommandBindings.Add(new CommandBinding(SaveFileCommand, SaveFile_Excute));
            CommandBindings.Add(new CommandBinding(NewFileCommand, NewFile_Excute));

        }
        public static RoutedCommand InitialCommand = new RoutedCommand();
        public static RoutedCommand RunCommand = new RoutedCommand();
        public static RoutedCommand StopCommand = new RoutedCommand();
        public static RoutedCommand LoginCommand = new RoutedCommand();
        public static RoutedCommand PauseCommand = new RoutedCommand();
        public static RoutedCommand ResetCommand = new RoutedCommand();
        public static RoutedCommand OpenFileCommand = new RoutedCommand();
        public static RoutedCommand SaveFileCommand = new RoutedCommand();
        public static RoutedCommand NewFileCommand = new RoutedCommand();



        private void Initial_Excute(object sender, ExecutedRoutedEventArgs e)
        {

        }


        private void Initial_CanExcute(object sender, CanExecuteRoutedEventArgs e)
        {
        }

        private void Run_Excute(object sender, ExecutedRoutedEventArgs e)
        {

        }

        private void Run_CanExcute(object sender, CanExecuteRoutedEventArgs e)
        {
        }

        private void Stop_Excute(object sender, ExecutedRoutedEventArgs e)
        {
        }

        private void Stop_CanExcute(object sender, CanExecuteRoutedEventArgs e)
        {

        }


        private void Login_Excute(object sender, ExecutedRoutedEventArgs e)
        {
            new WinLogInxaml().ShowDialog();
        }

        private void Pause_Excute(object sender, ExecutedRoutedEventArgs e)
        {
        }

        private void ResetCommand_Excute(object sender, ExecutedRoutedEventArgs e)
        {

        }

        private void ResetCommand_CanExcute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }


        private void NewFile_Excute(object sender, ExecutedRoutedEventArgs e)
        {
            WinRecip recipeDialog = new WinRecip();
            if (true == recipeDialog.ShowDialog())
            {
                model.ExcuteRecipe = new ProjectRecipe { Name = recipeDialog.RecipeName };
            }
        }

        private void OpenFile_Excute(object sender, ExecutedRoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.Multiselect = false;
            openFileDialog.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory + ProjectRecipe.FolderName;
            openFileDialog.Filter = $"(*{ProjectRecipe.PrjExten})|*{ProjectRecipe.PrjExten}";
            if (true == openFileDialog.ShowDialog())
            {
                string fileName = System.IO.Path.GetFileNameWithoutExtension(openFileDialog.FileName);
                model.ExcuteRecipe = ProjectRecipe.LoadProject(fileName);
            }
        }



        private void SaveFile_Excute(object sender, ExecutedRoutedEventArgs e)
        {
            if (null != model.ExcuteRecipe)
            {
                model.ExcuteRecipe.SavaProject();
            }
        }

    }
}
